
package edu.predatorwars.client;


import java.net.UnknownHostException;
import java.util.concurrent.LinkedBlockingQueue;

import edu.predatorwars.client.visualization.VisualizationCallbackInterface;
import edu.predatorwars.client.visualization.VisualizationStarter;
import edu.usm.predatorwars.ClientMask;
import edu.usm.predatorwars.ClientMaskBuilder;
import edu.usm.predatorwars.Playable;

public abstract class ClientBase implements VisualizationCallbackInterface, Playable {
	
	protected VisualizationStarter visualizationStarter;
	protected ClientMask clientmask;
	
	protected Object responseLock;
	protected boolean computerControlled;
	protected boolean running;
	
	protected boolean refresh_games;
	
	protected LinkedBlockingQueue<Runnable> clientWork;
	
	public ClientBase(ClientMask mask) {
		running = true;
		refresh_games = true;
		responseLock = new Object();
		
		visualizationStarter = new VisualizationStarter(this, "Predator Wars");
		clientWork = new LinkedBlockingQueue<Runnable>();
		clientmask = mask;
		
	}
	
	
	public void enterWaitForWorkLoop() {
		while (running) {
			Runnable toRun = null;
			try {
				toRun = clientWork.take();
			} catch (InterruptedException e) {}
			if (toRun != null) 
				toRun.run();
		}
	}
	
	public void postRunnable(Runnable run) {
		clientWork.offer(run);
	}
	
	protected static ClientMask buildClientMask(String port, String host) throws UnknownHostException {
		ClientMaskBuilder builder = new ClientMaskBuilder(host);
		return builder.hostPort(port).build();
	}
	
	protected abstract void doBehavior();
}